/*
 ============================================================================
 Name        : configParse.c
 Author      : xiaobo
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 this function can read from  configure, the file content format is  key=value.
 1.load configure file: 	loadFileConfigure(filename); the filename is configure file path
 2.read from configure file: char *val = getFileConfigVal("key"); the key is configure file's key, and not include space  and '\n' ot '\r'
   if read value is "" then the configure is error;

 */

#include <stdio.h>
#include <string.h>
typedef struct item_t {
	char *key;
	char *value;
} ITEM;

ITEM CONFIGFILEITEM[100];
int CONFIGFILEITEMNUM;
/* *clear all String space */

char *strClearAllNonChar(char *pstr) {

	int i = 0;
	i = strlen(pstr) - 1;
	char *tmpstr;
	tmpstr = (char *) malloc((strlen(pstr)+1)*sizeof(char));
	memset(tmpstr, 0, (strlen(pstr)+1));

	char *p = tmpstr;
	int k = 0;
	for(k = 0;k < i;k++) {

		if(!pstr[k]) break;

		if(!isspace(pstr[k])) {
			*p = pstr[k];
			p++;
		}
		if(pstr[k] == '\n' || pstr[k] == '\r') {
			continue;
		}
	}
	return tmpstr;
}
//check String is OK not head is * or # and not match a=b format
int checkValIsOK(char * valstr) {

	char *p = strClearAllNonChar(valstr);
	if(*p == '*' ||*p == '#') return 0;

	char *ptr = strchr(valstr, '=');
	if(ptr) return 1;
	return 0;
}

/* *从配置文件的一行读出key或value,返回item指针 *line--从配置文件读出的一行 */
int get_item_from_line(char *line, ITEM *item)
{
	char *p = strClearAllNonChar(line);
	int len = strlen(p);
	if(len <= 0) {
		return 1;//空行
	}
	else if(p[0]=='#') {return 2;}
	else {char *p2 = strchr(p, '=');
		*p2++ = '\0';
		item->key = (char *)malloc(strlen(p) + 1);
		item->value = (char *)malloc(strlen(p2) + 1);
		strcpy(item->key,p);
		strcpy(item->value,p2);
	}
	return 0;
	//查询成功
}


char * getFileConfigVal(char *valname) {

	char *p = "";
	int i = 0;
	for(i = 0; i < CONFIGFILEITEMNUM; i++ ) {

		if(strcmp(CONFIGFILEITEM[i].key, valname) == 0) {

			return CONFIGFILEITEM[i].value;
		}
	}
	return p;
}
//load config file
int loadFileConfigure(const char *file) {
	char line[1024];
	FILE *fp;
	fp = fopen(file,"r");
	if(fp == NULL)
	return 1;
	CONFIGFILEITEMNUM = 0;
	while(fgets(line, 1023, fp)) {
		char *p = line;
		if(!checkValIsOK(p)) continue;

		char *p2 = strchr(p, '=');
		/*这里认为只有key没什么意义*/
		if(p2 == NULL)
			continue;

		*p2++ = '\0';

		p2 = strClearAllNonChar(p2);

		int i = strlen(p2);

		CONFIGFILEITEM[CONFIGFILEITEMNUM].key = (char *)malloc(strlen(p) + 1);
		CONFIGFILEITEM[CONFIGFILEITEMNUM].value = (char *)malloc(strlen(p2) + 1);
		strcpy(CONFIGFILEITEM[CONFIGFILEITEMNUM].key,p);
		strcpy(CONFIGFILEITEM[CONFIGFILEITEMNUM].value,p2);
		CONFIGFILEITEMNUM++;
	}
	fclose(fp);
	return 0;
}

//int main(void) {
//
//	ITEM *item;
//	int i = 0;
//	char *filename = "./config.conf";
//	printf("begin work!");
//
//	loadFileConfigure(filename);
//	char *p = getFileConfigVal("c");
//	printf("the value is %s", p);
//	printf("end work!");
//}

